package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.mapper.CallAndGenerateCountsMapper;
import com.yupi.springbootinit.model.dto.count.QueryCountRequest;
import com.yupi.springbootinit.model.entity.CallAndGenerateCounts;
import com.yupi.springbootinit.model.vo.CallAndGenerateCountsVO;
import com.yupi.springbootinit.service.CallAndGenerateCountsService;
import com.yupi.springbootinit.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
* @author lfc
* @description 针对表【call_and_generate_counts】的数据库操作Service实现
* @createDate 2023-11-02 17:56:11
*/
@Service
public class CallAndGenerateCountsServiceImpl extends ServiceImpl<CallAndGenerateCountsMapper, CallAndGenerateCounts>
    implements CallAndGenerateCountsService {

    @Resource
    private RedisUtils redisUtils;

    private CallAndGenerateCountsVO cacheMapValue;

    @Override
    public CallAndGenerateCountsVO queryCount(QueryCountRequest queryCountRequest) {
        //先去redis中查询
        Long userId = queryCountRequest.getUserId();
        cacheMapValue= redisUtils.getCacheMapValue(userId.toString(), userId.toString());
        if (cacheMapValue == null){
            LambdaQueryWrapper<CallAndGenerateCounts> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CallAndGenerateCounts::getUserId,userId);
            CallAndGenerateCounts counts = this.getOne(wrapper);
            cacheMapValue = new CallAndGenerateCountsVO();
            BeanUtils.copyProperties(counts,cacheMapValue);
            String hKey = cacheMapValue.getUserId().toString();
            //没有的时候存储到redis当中
            redisUtils.setCacheMapValue(hKey,hKey,cacheMapValue);
            redisUtils.expire(hKey,30, TimeUnit.SECONDS);
        }
        return cacheMapValue;
    }

    @Override
    public Boolean updateCount(CallAndGenerateCounts counts) {
        Integer remainingGenerateCount = counts.getRemainingGenerateCount();
        Integer generatedCount = counts.getGeneratedCount();
        Integer remainTasks = counts.getRemainTasks();
        if(remainingGenerateCount<=0){
            throw  new BusinessException(ErrorCode.OPERATION_ERROR,"操作失败,生成次数不足尽快充值！！！");
        }
        //todo 当任务开始执行要待完成任务+1
        remainTasks = remainTasks +1;
        remainingGenerateCount = remainingGenerateCount-1;
        generatedCount = generatedCount+1;
        counts.setRemainingGenerateCount(remainingGenerateCount);
        counts.setGeneratedCount(generatedCount);
        counts.setRemainTasks(remainTasks);
        return this.updateById(counts);
    }

    @Override
    public CallAndGenerateCounts getByUserId(Long id) {
        LambdaQueryWrapper<CallAndGenerateCounts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CallAndGenerateCounts::getUserId,id);
        return this.getOne(queryWrapper);
    }
}




